<template>
  <div class="tool-bar-ri">
    <div class="header-icon">
      <Language id="language" />
      <SearchMenu id="searchMenu" />
      <el-tooltip content="主题切换" effect="dark" placement="bottom">
        <ThemeSetting id="themeSetting" />
      </el-tooltip>
      <el-tooltip content="全屏预览" effect="dark" placement="bottom">
        <Fullscreen id="fullscreen" />
      </el-tooltip>
    </div>
    <span class="username">{{ loginName }}</span>
    <Avatar />
  </div>
</template>

<script setup lang="ts">
import { computed, ref } from "vue";
import Language from "./Language.vue";
import SearchMenu from "./SearchMenu.vue";
import ThemeSetting from "./ThemeSetting.vue";
import Fullscreen from "./Fullscreen.vue";
import Avatar from "./Avatar.vue";
import actions from '../../stores/globalState';
import { getUserInfoFromStorage, getUserDisplayName } from '../../utils/userInfo';
import { getToken } from '../../utils/cookies';

// 用户信息响应式引用
const userInfo = ref<any>(null);

// 初始化用户信息
const initUserInfo = () => {
  console.log('[ToolBarRight] ===== 开始初始化用户信息 =====');
  
  // 检查 token
  const token = getToken();
  console.log('[ToolBarRight] ========================================');
  console.log('[ToolBarRight] Token 状态:', token ? `✅ 存在 (${token.substring(0, 20)}...)` : '❌ 不存在');
  console.log('[ToolBarRight] ========================================');
  
  // 先从 localStorage 获取
  const cachedUserInfo = getUserInfoFromStorage();
  console.log('[ToolBarRight] localStorage 中的用户信息:', cachedUserInfo);
  
  if (cachedUserInfo) {
    userInfo.value = cachedUserInfo;
    console.log('[ToolBarRight] ✅ 使用 localStorage 中的用户信息');
  } else {
    console.log('[ToolBarRight] ❌ localStorage 中没有用户信息');
  }
  
  // 尝试从全局状态获取
  const globalUserInfo = (actions as any).getGlobalState?.()?.user;
  console.log('[ToolBarRight] 全局状态中的用户信息:', globalUserInfo);
  
  if (globalUserInfo && globalUserInfo.id) {
    userInfo.value = globalUserInfo;
    console.log('[ToolBarRight] ✅ 使用全局状态中的用户信息');
  } else {
    console.log('[ToolBarRight] ❌ 全局状态中没有有效的用户信息');
  }
  
  console.log('[ToolBarRight] 最终的 userInfo.value:', userInfo.value);
  
  // 诊断提示
  if (!token) {
    console.warn('[ToolBarRight] ⚠️ 没有 token，用户可能未登录');
  }
  if (!userInfo.value) {
    console.warn('[ToolBarRight] ⚠️ 没有用户信息，请检查：');
    console.warn('  1. 是否已登录（是否有 token）');
    console.warn('  2. App.vue 中的 getUserData() 是否成功执行');
    console.warn('  3. 用户信息是否成功保存到 localStorage');
  }
  
  console.log('[ToolBarRight] ===== 用户信息初始化完成 =====');
};

// 初始化
initUserInfo();

// 监听全局状态变化
actions.onGlobalStateChange((state: any) => {
  if (state.user) {
    userInfo.value = state.user;
    console.log('[ToolBarRight] 全局状态变化，更新用户信息:', state.user);
  }
});

// 计算显示名称
const loginName = computed(() => {
  console.log('[ToolBarRight] 当前用户信息:', userInfo.value);
  return getUserDisplayName(userInfo.value);
});
</script>

<script lang="ts">
export default {
  name: 'ToolBarRight'
};
</script>

<style scoped lang="scss">
.tool-bar-ri {
  display: flex;
  align-items: center;
  gap: 16px; // Ant Design Pro 标准间距
  padding-right: 24px; // Ant Design Pro 标准边距

  .header-icon {
    display: flex;
    align-items: center;
    gap: 12px; // Ant Design Pro 图标间距

    * {
      height: 36px; // Ant Design Pro 标准图标高度
      width: 36px;
      min-width: 36px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1); // Ant Design 标准缓动
      font-size: 16px;
      color: rgba(0, 0, 0, 0.65);
      background-color: transparent;
      
      &:hover {
        background-color: rgba(0, 0, 0, 0.04); // Ant Design Pro hover 效果
        transform: scale(1.08);
        color: rgba(0, 0, 0, 0.85);
      }
      
      &:active {
        transform: scale(0.95);
      }
      
      :deep(.el-icon) {
        font-size: 16px;
        color: inherit;
      }
    }
  }

  .username {
    font-size: 14px; // Ant Design Pro 标准字号
    font-weight: 400;
    color: var(--el-text-color-primary);
    margin: 0 8px;
  }
}

// 暗黑模式适配
[data-theme='dark'] {
  .tool-bar-ri {
    .header-icon * {
      color: rgba(255, 255, 255, 0.65);
      
      &:hover {
        background-color: rgba(255, 255, 255, 0.08);
        color: rgba(255, 255, 255, 0.85);
      }
    }
  }
}
</style>
